package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.Patient;
import com.zhuiyi.yiqingbackend.domain.request.AddConfirmPatientRequest;
import com.zhuiyi.yiqingbackend.domain.request.AddPatientRequest;
import com.zhuiyi.yiqingbackend.domain.request.PatientUpdateStatusRequest;
import com.zhuiyi.yiqingbackend.domain.response.PatientResponse;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/patient")
@Api(value = "患者接口", tags = "患者管理接口")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class PatientController {

    @Resource
    private PatientService patientService;

    /**
     * 获取患者列表
     *
     */
    @GetMapping("/getPatients")
    @ApiOperation(value = "获取患者列表", notes = "获取患者列表")
    public BaseResponse<List<PatientResponse>> getPatients(){
        List<PatientResponse> patients = patientService.getPatients();
        return ResultUtils.success(patients);
    }

    /**
     * 添加患者
     *
     */
    @PostMapping("/addPatient")
    @ApiOperation(value = "添加患者", notes = "添加患者")
    public BaseResponse<Boolean> addPatient(@RequestBody AddPatientRequest request){
        boolean ok = patientService.addPatient(request);
        if(!ok){
            throw new BusinessException(ErrorCode.FAIL, "添加失败");
        }
        return ResultUtils.success(ok);
    }
    /**
     * 添加患者 plus
     *
     */
    @PostMapping("/addPatient2")
    @ApiOperation(value = "添加患者 plus", notes = "添加患者 plus")
    public BaseResponse<Boolean> addPatient2(@RequestBody AddConfirmPatientRequest request){
        boolean ok = patientService.addPatient2(request);
        return ResultUtils.success(ok);
    }

    /**
     * 删除患者
     *
     */
    @GetMapping("/deletePatient/{id}")
    @ApiOperation(value = "删除患者", notes = "删除患者")
    public BaseResponse deletePatient(@PathVariable int id){

        return ResultUtils.success(patientService.deletePatientById(id));
    }

    /**
     * 更新患者
     *
     */
    @PostMapping("/updatePatient")
    @ApiOperation(value = "更新患者", notes = "更新患者")
    public BaseResponse updatePatient(@RequestBody Patient patient){
        return ResultUtils.success(patientService.updatePatient(patient));
    }
    /**
     * 更新患者
     *
     */
    @PostMapping("/updatePatientStatus")
    @ApiOperation(value = "更新患者 状态", notes = "更新患者状态")
    public BaseResponse updatePatient(@RequestBody PatientUpdateStatusRequest request){
        return ResultUtils.success(patientService.updateStatus(request));
    }

    /**
     * 获取患者详情
     */
    @GetMapping("/getPatientById/{id}")
    @ApiOperation(value = "获取患者详情", notes = "获取患者详情")
    public BaseResponse<PatientResponse> getPatientById(@PathVariable int id){
        return ResultUtils.success(patientService.getPatientById(id));
    }
}
